package de.hs.mmk.group_talk.audio_managing;


import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.util.Log;



public class AudioRecorder extends Thread {
	
	boolean isRecording = false;
	File file = new File(AudioPreferences.FILE_DIRECTORY, AudioPreferences.FILE_NAME);
	AudioRecord audioRecord;
	AudioStreamer audioStreamer = null;
	int bufferSize;
	byte[] buffer;
	DataOutputStream dos; 	
	private final static int SERVR_PORT = 1235;
	private final static int MY_PORT = 4000;
	private final static String SERVER_IP = "172.30.104.2";
	private DatagramSocket mySocket = null;
	
	
	public AudioRecorder() { 	
		android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
    }
	
	public void startRecord(){
		start();
	}
	
	
	@Override
	public void run(){
		try{
			file.createNewFile();
	    }catch (IOException e) {}
	         
	    try {
			this.mySocket = new DatagramSocket(MY_PORT);
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   
			
		DatagramPacket datagramPacket;   
	    
	    try{
	    	
	    	OutputStream os = new FileOutputStream(file);
	        BufferedOutputStream bos = new BufferedOutputStream(os);
	        dos = new DataOutputStream(bos);
	             
	        bufferSize = AudioRecord.getMinBufferSize(
	        		AudioPreferences.FREQUENCY, 
	        		AudioPreferences.CHANNEL_CONFIG,
	        		AudioPreferences.AUDIO_ENCODING);
	        
	        Log.d("FOUAD", "Buffersize: "+bufferSize);
	        
	        buffer = new byte[bufferSize];
	        audioRecord = new AudioRecord(
	        		MediaRecorder.AudioSource.MIC, 
	        		AudioPreferences.FREQUENCY, 
	        		AudioPreferences.CHANNEL_CONFIG,
	        		AudioPreferences.AUDIO_ENCODING,
	        		bufferSize);
	        
	        audioRecord.startRecording();
	        isRecording = true;
	        
	        datagramPacket = new DatagramPacket(new byte[1], 1, InetAddress.getByName(SERVER_IP), SERVR_PORT);
	        
	        while (isRecording) {
	        	
	        	
	        	int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
	          	//byte[] rtpPacketBytes = rtpPacket.getRtpPacket();
	        	
				datagramPacket.setData(buffer);
				System.out.println("Size: "+datagramPacket.getData().length);				
				this.mySocket.send(datagramPacket);
	        	
	        	
	            for (int i = 0; i < bufferReadResult; i++){
	            	dos.writeByte(buffer[i]);
	            	//if(audioStreamer == null){
	            	//	audioStreamer = new AudioStreamer(mySocket);
	            	//}
	            }
	        }
	    } catch(Throwable x) { 
            Log.d("TAG", "ERROR:",x);
        } finally { 
        	audioRecord.stop();
            try {
				dos.close();
			} catch (IOException e) {
				Log.d("TAG", "IO", e);
			}
        } 
	}
	
	public void stopRecord() { 
        isRecording = false;
    }
	
	public void send(RtpPacket rtpPacket) {		

		byte[] rtpPacketBytes = rtpPacket.getRtpPacket();		
		DatagramPacket datagramPacket;
		
		try {
			datagramPacket = new DatagramPacket(new byte[1], 1, InetAddress.getByName(SERVER_IP), SERVR_PORT);
			datagramPacket.setData(rtpPacketBytes);
			System.out.println("Size: "+datagramPacket.getData().length);
			
			this.mySocket.send(datagramPacket);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
